package com.bytecodezz.app.base.toolset.color

import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/9/2 13:01
 * @package: com.bytecodezz.app.base.toolset.color
 *
 * @description:
 *
 */
class ColorTools {

    companion object {

        /**
         * 给color添加透明度
         * @param alpha 透明度 0f～1f
         * @param baseColor 基本颜色
         * @return
         */
        @ColorInt
        fun reviseAlpha(
            @FloatRange(from = 0.0, to = 1.0) alpha: Float,
            @ColorInt baseColor: Int
        ): Int {
            val a = 255.coerceAtMost(0.coerceAtLeast((alpha * 255).toInt())) shl 24
            val rgb = 0x00ffffff and baseColor
            return a + rgb
        }

        /**
         * 混合2个颜色，按ratio比例混合
         */
        @ColorInt
        fun blendColor(
            @ColorInt startColor: Int,
            @ColorInt endColor: Int,
            @FloatRange(from = 0.0, to = 1.0) ratio: Float
        ): Int {
            val inverseRatio = 1 - ratio
            val a: Float = Color.alpha(startColor) * inverseRatio + Color.alpha(endColor) * ratio
            val r: Float = Color.red(startColor) * inverseRatio + Color.red(endColor) * ratio
            val g: Float = Color.green(startColor) * inverseRatio + Color.green(endColor) * ratio
            val b: Float = Color.blue(startColor) * inverseRatio + Color.blue(endColor) * ratio
            return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt())
        }
    }

}